package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.MsElderVo;
import org.dromara.nursing.domain.bo.MsElderBo;
import org.dromara.nursing.service.IMsElderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 老人基础信息
 *
 * @author Xue
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/ms_elder")
public class MsElderController extends BaseController {

    private final IMsElderService msElderService;

    /**
     * 查询老人基础信息列表
     */
    @SaCheckPermission("nursing:ms_elder:list")
    @GetMapping("/list")
    public TableDataInfo<MsElderVo> list(MsElderBo bo, PageQuery pageQuery) {
        return msElderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出老人基础信息列表
     */
    @SaCheckPermission("nursing:ms_elder:export")
    @Log(title = "老人基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsElderBo bo, HttpServletResponse response) {
        List<MsElderVo> list = msElderService.queryList(bo);
        ExcelUtil.exportExcel(list, "老人基础信息", MsElderVo.class, response);
    }

    /**
     * 获取老人基础信息详细信息
     *
     * @param elderId 主键
     */
    @SaCheckPermission("nursing:ms_elder:query")
    @GetMapping("/{elderId}")
    public R<MsElderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long elderId) {
        return R.ok(msElderService.queryById(elderId));
    }

    /**
     * 新增老人基础信息
     */
    @SaCheckPermission("nursing:ms_elder:add")
    @Log(title = "老人基础信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsElderBo bo) {
        return toAjax(msElderService.insertByBo(bo));
    }

    /**
     * 修改老人基础信息
     */
    @SaCheckPermission("nursing:ms_elder:edit")
    @Log(title = "老人基础信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsElderBo bo) {
        return toAjax(msElderService.updateByBo(bo));
    }

    /**
     * 删除老人基础信息
     *
     * @param elderIds 主键串
     */
    @SaCheckPermission("nursing:ms_elder:remove")
    @Log(title = "老人基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{elderIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] elderIds) {
        return toAjax(msElderService.deleteWithValidByIds(List.of(elderIds), true));
    }
}
